<template>
  <div class="user-list">
    <h2>用户列表</h2>

    <div class="filters">
      <input v-model="searchQuery" placeholder="搜索用户..." class="search-input" />
      <select v-model="roleFilter" class="role-filter">
        <option value="">所有角色</option>
        <option value="admin">管理员</option>
        <option value="user">普通用户</option>
        <option value="moderator">版主</option>
      </select>
    </div>

    <div class="users-grid">
      <user-card
        v-for="user in filteredUsers"
        :key="user.id"
        :user="user"
        :is-active="selectedUser?.id === user.id"
        @edit="handleEditUser"
        @delete="handleDeleteUser"
      />
    </div>

    <!-- 编辑用户模态框 -->
    <div v-if="showEditModal" class="modal-overlay" @click="closeEditModal">
      <div class="modal" @click.stop>
        <h3>编辑用户</h3>
        <form @submit.prevent="saveUser" v-if="editingUser">
          <div class="form-group">
            <label>姓名:</label>
            <input v-model="editingUser.name" required />
          </div>
          <div class="form-group">
            <label>邮箱:</label>
            <input v-model="editingUser.email" type="email" required />
          </div>
          <div class="form-group">
            <label>角色:</label>
            <select v-model="editingUser.role" required>
              <option value="user">普通用户</option>
              <option value="moderator">版主</option>
              <option value="admin">管理员</option>
            </select>
          </div>
          <div class="modal-actions">
            <button type="button" @click="closeEditModal">取消</button>
            <button type="submit">保存</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import UserCard from '@/components/UserCard.vue'

interface User {
  id: number
  name: string
  email: string
  role: string
  avatar: string
  online: boolean
}

const users = ref([
  {
    id: 1,
    name: '张三',
    email: 'zhangsan@example.com',
    role: 'admin',
    avatar: 'https://via.placeholder.com/50',
    online: true,
  },
  {
    id: 2,
    name: '李四',
    email: 'lisi@example.com',
    role: 'user',
    avatar: 'https://via.placeholder.com/50',
    online: false,
  },
  {
    id: 3,
    name: '王五',
    email: 'wangwu@example.com',
    role: 'moderator',
    avatar: 'https://via.placeholder.com/50',
    online: true,
  },
])

const searchQuery = ref('')
const roleFilter = ref('')
const selectedUser = ref<User | null>(null)
const showEditModal = ref(false)
const editingUser = ref<User | null>(null)

const filteredUsers = computed(() => {
  return users.value.filter((user) => {
    const matchesSearch =
      user.name.toLowerCase().includes(searchQuery.value.toLowerCase()) ||
      user.email.toLowerCase().includes(searchQuery.value.toLowerCase())
    const matchesRole = !roleFilter.value || user.role === roleFilter.value
    return matchesSearch && matchesRole
  })
})

const handleEditUser = (user: User) => {
  editingUser.value = { ...user }
  showEditModal.value = true
}

const handleDeleteUser = (userId: number) => {
  const index = users.value.findIndex((user) => user.id === userId)
  if (index > -1) {
    users.value.splice(index, 1)
  }
}

const closeEditModal = () => {
  showEditModal.value = false
  editingUser.value = null
}

const saveUser = () => {
  if (!editingUser.value) return
  const user = editingUser.value as User
  const index = users.value.findIndex((u) => u.id === user.id)
  if (index > -1) {
    users.value[index] = { ...user }
  }
  closeEditModal()
}
</script>

<style scoped>
.user-list {
  padding: 20px;
}

.filters {
  display: flex;
  gap: 15px;
  margin-bottom: 20px;
}

.search-input,
.role-filter {
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.search-input {
  flex: 1;
}

.users-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 15px;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal {
  background: white;
  padding: 20px;
  border-radius: 8px;
  min-width: 400px;
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: bold;
}

.form-group input,
.form-group select {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 20px;
}

.modal-actions button {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.modal-actions button:first-child {
  background: #6c757d;
  color: white;
}

.modal-actions button:last-child {
  background: #007bff;
  color: white;
}
</style>
